package com.chy.blog_web.modular.web.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.chy.blog_web.core.util.PageAndParamUtil;
import com.chy.blog_web.modular.system.entity.SysMenu;
import com.chy.blog_web.modular.system.service.ISysMenuService;
import com.chy.blog_web.modular.web.entity.BoArticle;
import com.chy.blog_web.modular.web.entity.BoContent;
import com.chy.blog_web.modular.web.entity.BoPersonalDiary;
import com.chy.blog_web.modular.web.entity.BoPhotoAlbum;
import com.chy.blog_web.modular.web.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：我的日记 controller
 * 作者：chenhuiyan
 * 时间：2020/03/08 22:07
 */
@Controller
@RequestMapping(value = "/myDiary")
public class MyDiaryController {

    /**
     * 前缀
     */
    private static String PREFIX = "web/myDiary/";

    /**
     * 导航service
     */
    @Autowired
    private ISysMenuService sysMenuService;

    /**
     * 内容service
     */
    @Autowired
    private IBoContentService boContentService;

    /**
     * 相册service
     */
    @Autowired
    private IBoPhotoAlbumService boPhotoAlbumService;

    /**
     * 文章标签service
     */
    @Autowired
    private IBoTagService boTagService;

    /**
     * 文章service
     */
    @Autowired
    private IBoArticleService boArticleService;

    /**
     * 日记service
     */
    @Autowired
    private IBoPersonalDiaryService boPersonalDiaryService;

    /**
     * 跳转我的日记页面
     * @return
     */
    @RequestMapping(value = "")
    public String index(){
        return PREFIX + "myDiary";
    }

    /**
     * 查询个人日记列表
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getGDiaryList")
    public Map<String, Object> getGDiaryList(){
        Map<String, Object> res = new HashMap<>();
        try {
            Map<String, Object> params = PageAndParamUtil.getParams();
            //查询日记类型为个人日记列表，并按时间降序排列
            List<Map<String, Object>> diaryList = boPersonalDiaryService.list(params);
            Integer count = boPersonalDiaryService.getBaseMapper().selectCount(new QueryWrapper<BoPersonalDiary>().eq("type", 0).eq("del_flag", 0)); //获取总数
            res.put("state", "200");
            res.put("message", "查询日记列表成功");
            res.put("diaryList", diaryList);
            res.put("count", count);
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "服务器异常，请重新刷新页面！");
        }
        return res;
    }

    /**
     * 查询隐私日记列表，先进行密码验证再查询
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getYDiaryList")
    public Map<String, Object> getYDiaryList(){
        Map<String, Object> res = new HashMap<>();
        try {
            //查询前先进行密码验证
            boolean flag = validPassword();
            if(!flag){
                res.put("state", "500");
                res.put("message", "隐私日记已加锁");
                return res;
            }
            Map<String, Object> params = PageAndParamUtil.getParams();
            //查询日记类型为隐私日记列表，并按时间降序排列
            List<Map<String, Object>> diaryList = boPersonalDiaryService.list(params);
            Integer count = boPersonalDiaryService.getBaseMapper().selectCount(new QueryWrapper<BoPersonalDiary>().eq("type", 1).eq("del_flag", 0)); //获取总数
            res.put("state", "200");
            res.put("message", "查询日记列表成功");
            res.put("diaryList", diaryList);
            res.put("count", count);
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "服务器异常，请重新刷新页面！");
        }
        return res;
    }

    /**
     * 验证隐私日记密码，验证通过则保存Session
     * @param diaryPassword
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/validDiaryPassword")
    public Map<String, Object> validDiaryPassword(@RequestParam(name = "diaryPassword") String diaryPassword){
        Map<String, Object> res = new HashMap<>();
        try {
            //密码验证
            //内容保存密码的编号：a0edf31ba84a4517bfb36dc7bbe37732
            BoContent boContent = boContentService.getById("a0edf31ba84a4517bfb36dc7bbe37732");
            if(diaryPassword.equals(boContent.getTitle())){
                HttpServletRequest request = PageAndParamUtil.getRequest();
                HttpSession session = request.getSession();
                session.setAttribute("diaryPassword", diaryPassword); //密码验证通过，保存session
                res.put("state", "200");
                res.put("message", "密码验证通过");
            }else{
                res.put("state", "500");
                res.put("message", "密码验证不通过");
            }
        }catch (Exception e){
            e.printStackTrace();
            res.put("state", "500");
            res.put("message", "服务器异常，请重新刷新页面！");
        }
        return res;
    }

    /**
     * 跳转日记详情页面
     * @return
     */
    @RequestMapping(value = "diaryInfo")
    public String diaryInfo(@RequestParam(name = "diaryId") String id, Model model){
        model.addAttribute("id", id);
        model.addAttribute("type", "diary"); //日记标识
        return "web/myArticle/" + "articleInfo";
    }

    /**
     * 根据日记编号查询日记信息，类型为隐私日记则进行密码校验
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getDiaryInfo")
    public Map<String, Object> getDiaryInfo(@RequestParam(name = "id") String id){
        Map<String, Object> res = new HashMap<>();
        try {
            Map<String, Object> diaryInfo = boPersonalDiaryService.getDiaryInfo(id);
            if(1 == Integer.parseInt(diaryInfo.get("type") + "")){
                boolean flag = validPassword();
                if(!flag){
                    res.put("state", "500");
                    res.put("message", "隐私日记不能查看");
                    return res;
                }
            }
            res.put("state", "200");
            res.put("message", "查询文章详情成功");
            res.put("diaryInfo", diaryInfo);
        }catch (Exception e){
            res.put("state", "500");
            res.put("message", "服务器异常，请重新刷新页面！");
            e.printStackTrace();
        }
        return res;
    }

    /**
     * 进行类型为隐私日记密码校验
     * @return
     */
    public boolean validPassword(){
        //内容保存密码的编号：a0edf31ba84a4517bfb36dc7bbe37732
        BoContent boContent = boContentService.getById("a0edf31ba84a4517bfb36dc7bbe37732");
        HttpServletRequest request = PageAndParamUtil.getRequest();
        HttpSession session = request.getSession();
        String diaryPassword = session.getAttribute("diaryPassword") + "";
        if(diaryPassword == null || diaryPassword == ""){
            return false;
        }else if(diaryPassword.equals(boContent.getTitle())){
            return true;
        }
        return false;
    }

}
